﻿<%@ WebHandler Language="C#" Class="KanbanHandler" %>

using System;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using System.Collections.Generic;
using uMES.LeanManufacturing.ReportBusiness;
using System.Web.SessionState;
using uMES.LeanManufacturing.ParameterDTO;
using System.Configuration;
using System.Linq;

public class KanbanHandler : IHttpHandler, IReadOnlySessionState
{
    private uMESKanbanFunctionBusiness bll = new uMESKanbanFunctionBusiness();
    public void ProcessRequest(HttpContext context)
    {
        if (context.Server.UrlDecode(context.Request["type"]) != null)
        {
            //分厂生产看板--分厂月度生产统计
            if (context.Server.UrlDecode(context.Request["type"]) == "ProductionStatistics")
            {
                ProductionStatistics(context);
            }

            //分厂生产看板--班组合格率统计
            if (context.Server.UrlDecode(context.Request["type"]) == "QualRateStatistics")
            {
                QualRateStatistics(context);
            }

            //分厂生产看板--班组完成率统计
            if (context.Server.UrlDecode(context.Request["type"]) == "FinishRateStatistics")
            {
                FinishRateStatistics(context);
            }

            //分厂生产看板--质量缺陷分析
            if (context.Server.UrlDecode(context.Request["type"]) == "QualDefectionAnalysis")
            {
                QualDefectionAnalysis(context);
            }

            //生产问题处理看板——表格信息
            if (context.Server.UrlDecode(context.Request["type"]) == "ProductionProblemDispose1")
            {
                ProductionProblemDispose1(context);
            }

            //生产问题处理看板——回复率、完成率、超期率
            if (context.Server.UrlDecode(context.Request["type"]) == "ProductionProblemDispose2")
            {
                ProductionProblemDispose2(context);
            }

            //所有分厂
            if (context.Server.UrlDecode(context.Request["type"]) == "AllFactory")
            {
                AllFactory(context);
            }

            if (context.Server.UrlDecode(context.Request["type"]) == "GetWorkReport")
            {
                GetWorkReport(context);
            }
        }
    }

    //分厂月度生产统计
    private void ProductionStatistics(HttpContext context)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        if (context.Request["factoryid"] != null)
        {
            string factoryid = context.Server.UrlDecode(context.Request["factoryid"]).ToString().Trim();
            para.Add("FactoryID", factoryid);
        }
        if (context.Request["teamid"] != null)
        {
            string teamid = context.Server.UrlDecode(context.Request["teamid"]).ToString().Trim();
            para.Add("TeamID", teamid);
        }
        string[] months = new string[6];
        for (int i = 0; i < months.Length; i++)
        {
            months[i] = DateTime.Parse(context.Request["month"]).AddMonths(-i).ToString("yyyy-MM");
        }
        para.Add("Time", string.Join("','", months));
        DataTable dt = bll.GetProductionStatistics(para);
        var json = JsonConvert.SerializeObject(dt);
        context.Response.ContentType = "text/plain";
        json = "{\"arr\":" + json.ToString() + "}";
        context.Response.Write(json.ToString());
        context.Response.End();
    }

    //-班组合格率统计
    private void QualRateStatistics(HttpContext context)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("Time", DateTime.Parse(context.Request["month"]).ToString("yyyy-MM"));
        if (context.Request["factoryid"] != null)
        {
            string factoryid = context.Server.UrlDecode(context.Request["factoryid"]).ToString().Trim();
            para["FactoryID"] = factoryid;
        }
        if (context.Request["teamid"] != null)
        {
            string teamid = context.Server.UrlDecode(context.Request["teamid"]).ToString().Trim();
            para.Add("TeamID", teamid);
        }
        DataTable dt = bll.GetQualRateStatisticsInfo(para);
        var json = JsonConvert.SerializeObject(dt);
        context.Response.ContentType = "text/plain";
        json = "{\"arr\":" + json.ToString() + "}";
        context.Response.Write(json.ToString());
        context.Response.End();
    }

    //班组完成率统计
    private void FinishRateStatistics(HttpContext context)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("Time", DateTime.Parse(context.Request["month"]).ToString("yyyy-MM"));
        if (context.Request["factoryid"] != null)
        {
            string factoryid = context.Server.UrlDecode(context.Request["factoryid"]).ToString().Trim();
            para["FactoryID"] = factoryid;
        }
        if (context.Request["teamid"] != null)
        {
            string teamid = context.Server.UrlDecode(context.Request["teamid"]).ToString().Trim();
            para.Add("TeamID", teamid);
        }
        DataTable dt = bll.GetFinishRateStatisticsInfo(para);
        var json = JsonConvert.SerializeObject(dt);
        context.Response.ContentType = "text/plain";
        json = "{\"arr\":" + json.ToString() + "}";
        context.Response.Write(json.ToString());
        context.Response.End();
    }

    //质量缺陷分析
    private void QualDefectionAnalysis(HttpContext context)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("Time", DateTime.Parse(context.Request["month"]).ToString("yyyy-MM"));
        if (context.Request["factoryid"] != null)
        {
            string factoryid = context.Server.UrlDecode(context.Request["factoryid"]).ToString().Trim();
            para["FactoryID"] = factoryid;
        }
        if (context.Request["teamid"] != null)
        {
            string teamid = context.Server.UrlDecode(context.Request["teamid"]).ToString().Trim();
            para.Add("TeamID", teamid);
        }
        DataTable dt = bll.GetGetQualDefectionAnalysisInfo(para);
        var json = JsonConvert.SerializeObject(dt);
        context.Response.ContentType = "text/plain";
        json = "{\"arr\":" + json.ToString() + "}";
        context.Response.Write(json.ToString());
        context.Response.End();
    }

    //表格信息
    private void ProductionProblemDispose1(HttpContext context)
    {
        int intCurrentPage = 0;
        int intPageSize = 0;
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("Time", DateTime.Now.ToString("yyyy-MM"));
        if (context.Request["currentPage"] != null)
        {
            intCurrentPage = int.Parse(context.Request["currentPage"].ToString());
        }
        if (context.Request["pageSize"] != null)
        {
            intPageSize = int.Parse(context.Request["pageSize"].ToString());
        }
        if (context.Request["factoryid"] != null && context.Request["IsCurrentFactory"] == "1")
        {
            para["FactoryID"] = context.Request["factoryid"].ToString();
        }
        if (context.Request["teamid"] != null)
        {
            para["TeamID"] = context.Request["teamid"].ToString();
        }
        uMESPagingDataDTO retR = bll.GetProductionProblemDisposeData1(para, intCurrentPage, intPageSize);
        DataTable dt = new DataTable();
        dt.Columns.Add("problemdetails");//问题描述
        dt.Columns.Add("problemtypename");//问题类型
        dt.Columns.Add("factoryname");//责任单位
        dt.Columns.Add("toName");//责任人
        dt.Columns.Add("disposeName");//实际处理人                              ;
        dt.Columns.Add("submitName");//创建人
        dt.Columns.Add("problemStatus");//问题状态
        dt.Columns.Add("DemandDegree");//需求程度
        dt.Columns.Add("OverdueTime");//超期时间
        if (retR.DBTable.Rows.Count > 0)
        {
            foreach (DataRow dr in retR.DBTable.Rows)
            {
                dt.Rows.Add();
                dt.Rows[dt.Rows.Count - 1]["problemdetails"] = "";
                if (!string.IsNullOrEmpty(dr["problemdetails"].ToString()))
                {
                    dt.Rows[dt.Rows.Count - 1]["problemdetails"] = dr["problemdetails"].ToString();
                }
                if (!string.IsNullOrEmpty(dr["problemtypename"].ToString()))
                {
                    dt.Rows[dt.Rows.Count - 1]["problemtypename"] = dr["problemtypename"].ToString();
                }
                if (!string.IsNullOrEmpty(dr["factoryname"].ToString()))
                {
                    dt.Rows[dt.Rows.Count - 1]["factoryname"] = dr["factoryname"].ToString();
                }
                if (!string.IsNullOrEmpty(dr["toName"].ToString()))
                {
                    dt.Rows[dt.Rows.Count - 1]["toName"] = dr["toName"].ToString();
                }
                if (!string.IsNullOrEmpty(dr["disposeName"].ToString()))
                {
                    dt.Rows[dt.Rows.Count - 1]["disposeName"] = dr["disposeName"].ToString();
                }
                if (!string.IsNullOrEmpty(dr["submitName"].ToString()))
                {
                    dt.Rows[dt.Rows.Count - 1]["submitName"] = dr["submitName"].ToString();
                }
                if (!string.IsNullOrEmpty(dr["problemStatus"].ToString()))
                {
                    dt.Rows[dt.Rows.Count - 1]["problemStatus"] = dr["problemStatus"].ToString();
                }
                if (!string.IsNullOrEmpty(dr["DemandDegree"].ToString()))
                {
                    dt.Rows[dt.Rows.Count - 1]["DemandDegree"] = dr["DemandDegree"].ToString();
                }
                if (!string.IsNullOrEmpty(dr["OverdueTime"].ToString()))
                {
                    dt.Rows[dt.Rows.Count - 1]["OverdueTime"] = dr["OverdueTime"].ToString();
                }
            }
        }
        var json = JsonConvert.SerializeObject(dt);
        context.Response.ContentType = "text/plain";
        json = "{\"arr\":" + json.ToString() + ",\"pageNum\":" + retR.PageCount + ",\"rowCount\":" + retR.RowCount + "}";
        context.Response.Write(json.ToString());
        context.Response.End();
    }

    private void ProductionProblemDispose2(HttpContext context)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("Time", DateTime.Now.ToString("yyyy-MM"));
        if (context.Request["factoryid"] != null && context.Request["IsCurrentFactory"] == "1")
        {
            para["FactoryID"] = context.Request["factoryid"].ToString();
        }
        if (context.Request["teamid"] != null)
        {
            para["TeamID"] = context.Request["teamid"].ToString();
        }
        DataTable dt = bll.GetProductionProblemDisposeData2(para);
        var json = JsonConvert.SerializeObject(dt);
        context.Response.ContentType = "text/plain";
        json = "{\"arr\":" + json.ToString() + "}";
        context.Response.Write(json.ToString());
        context.Response.End();
    }

    private void AllFactory(HttpContext context)
    {
        uMESCommonBusiness common = new uMESCommonBusiness();
        DataTable dtFactory = common.GetFactoryNames();
        string[] factorys = ConfigurationManager.AppSettings["Factorys"].Split(';');
        List<DataRow> drFactorys = dtFactory.AsEnumerable().Where(t => factorys.Contains(t["DESCRIPTION"].ToString())).OrderBy(t => t["FACTORYNAME"].ToString()).ToList();
        List<Dictionary<string, string>> paras = new List<Dictionary<string, string>>();
        for (int i = 0; i < drFactorys.Count; i++)
        {
            Dictionary<string, string> para = new Dictionary<string, string>();
            para.Add("FACTORYID", drFactorys[i]["FACTORYID"].ToString());
            para.Add("DESCRIPTION", drFactorys[i]["DESCRIPTION"].ToString());
            paras.Add(para);
        }
        var json = JsonConvert.SerializeObject(paras);
        context.Response.ContentType = "text/plain";
        json = "{\"arr\":" + json.ToString() + "}";
        context.Response.Write(json.ToString());
        context.Response.End();
    }

    private void GetWorkReport(HttpContext context)
    {
        string factoryid = context.Request["factoryid"];
        string teamid = context.Request["teamid"];
        string month = context.Request["month"];
        DataTable dt = bll.GetWorkReport(factoryid, teamid, month);
        var json = JsonConvert.SerializeObject(dt);
        context.Response.ContentType = "text/plain";
        json = "{\"arr\":" + json.ToString() + "}";
        context.Response.Write(json.ToString());
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}